//
//  RootViewController.m
//  LNA
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//


#import "RootViewController.h"
#import <AFNetworking.h>
#import "CarouselPicture.h"
#import "RootModel.h"
#import <UIImageView+WebCache.h>
#import "RootCell.h"
#import "CellViewController.h"
#import "FirstViewController.h"
#import "BtnJumpViewController.h"
#import "LunbotuViewController.h"

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate,CarouselPictureDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) NSMutableArray *scrollArr;// 轮播图图片数组
@property (nonatomic, retain) NSMutableArray *urlArr; // 轮播图图片数据
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIView *header;
@property (nonatomic, retain) NSMutableArray *picArr;//发现下一站图片数组;
@property (nonatomic, retain) NSMutableArray *urlPicArr;//发现下一站图片数据
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 推荐页面VC
    self.navigationItem.title = @"推荐";
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    // cell 重用池
    [_tableView registerClass:[RootCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];
    
    // 轮播图调用方法
    [self pic];
    
    // tableheader
    self.header = [[UIView alloc] initWithFrame:CGRectMake(10, 284, self.view.frame.size.width, 640)];
//    _header.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = _header;
    // 发现下一站
    [self imgView];
    [_header release];
    
    // 自定义cell数据
    [self data];
}
#pragma mark - 首页轮播图
- (void)pic
{
    // 获取轮播图数据
    NSString *str = @"http://open.qyer.com/qyer/recommands/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88243146147855&lon=121.5398488177902&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone7%2C2&track_deviceid=0C79B4F1-2C09-4E9A-805E-05836431AB22&track_os=ios%25209.2&v=1";
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    //    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [sessionManager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"%lld", downloadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //  在这里面写 数据处理!!!
        
        NSMutableArray *arr = responseObject[@"data"][@"slide"];
        self.scrollArr = [NSMutableArray array];
        self.urlArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            // 轮播图图片数组
            [self.scrollArr addObject:dic[@"photo"]];
            // 轮播图图片数据
            [self.urlArr addObject:dic[@"url"]];
        }
        // 下一站图片数组
        self.picArr = [NSMutableArray array];
        self.picArr = responseObject[@"data"][@"subject"];
        // 发现下一站
        [self imgView];
        // 轮播图
        CarouselPicture *pic = [[CarouselPicture alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) timeInterval:3 imageUrlArr:_scrollArr urlArr:_urlArr];
        [self.header addSubview:pic];
        pic.delegate = self;
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}
#pragma mark - 轮播图协议实现 方法
- (void)pushWithUrl:(NSString *)url
{
    LunbotuViewController *lunbotuVC = [[LunbotuViewController alloc] init];
    lunbotuVC.str = url;
    [self.navigationController pushViewController:lunbotuVC animated:YES];
    
}
#pragma mark - 发现下一站
- (void)imgView
{
    
   
    
    
    // 发现下一站三个图
    UIImageView *imgView = [[UIImageView alloc] init];
//    imgView.backgroundColor = [UIColor yellowColor];
    imgView.frame = CGRectMake(10, 240, self.view.frame.size.width - 20, 150);
    imgView.layer.cornerRadius = 10;
    imgView.clipsToBounds = YES;
    [imgView sd_setImageWithURL:[NSURL URLWithString:_picArr[0][@"photo"]]];
    
    // 创建轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    tap.numberOfTapsRequired = 1;
    [imgView addGestureRecognizer:tap];
    // 用户交互
    imgView.userInteractionEnabled = YES;
    [tap release];

    UIImageView *imgViewone = [[UIImageView alloc] init];
//    imgView1.backgroundColor = [UIColor yellowColor];
    imgViewone.frame = CGRectMake(10, 410, 165, 150);
    imgViewone.layer.cornerRadius = 10;
    imgViewone.clipsToBounds = YES;
    [imgViewone sd_setImageWithURL:[NSURL URLWithString:_picArr[1][@"photo"]]];
    UITapGestureRecognizer *tapone = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionone:)];
    tapone.numberOfTapsRequired = 1;
    [imgViewone addGestureRecognizer:tapone];
    // 用户交互
    imgViewone.userInteractionEnabled = YES;
    [tapone release];
    
    
    
    
    
    UIImageView *imgViewSec = [[UIImageView alloc] init];
//    imgView2.backgroundColor = [UIColor yellowColor];
    imgViewSec.frame = CGRectMake(200, 410 , 165, 150);
    imgViewSec.layer.cornerRadius = 10;
    imgViewSec.clipsToBounds = YES;
    [imgViewSec sd_setImageWithURL:[NSURL URLWithString:_picArr[2][@"photo"]]];
    UITapGestureRecognizer *tapSec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionSec:)];
    tapSec.numberOfTapsRequired = 1;
    [imgViewSec addGestureRecognizer:tapSec];
    imgViewSec.userInteractionEnabled = YES;
    [tapSec release];
    
    // 发现下一站label(当你发现这句话的时候 你一定会认为是你同桌我)
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 205, 150, 30)];
    label.text = @"发现下一站";
    label.textColor = [UIColor grayColor];
    
    // 查看更多精彩专题 button点击
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(130, 570, 120, 30);
//    [_header addSubview:btn];
    [btn setTitle:@"查看更多精彩专题 ☞" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_header addSubview:btn];
    // btn 方法
   [btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
    

    // 观看热门游记label
    UILabel *lookLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 600, 150, 30)];
//    lookLabel.backgroundColor = [UIColor yellowColor];
    lookLabel.text = @"看热门游记";
    lookLabel.textColor = [UIColor grayColor];
    
    [self.header addSubview:lookLabel];
    [self.header addSubview:imgViewSec];
    [self.header addSubview:imgViewone];
    [self.header addSubview:label];
    [self.header addSubview:imgView];
    
    
    [lookLabel release];
    [imgViewSec release];
    [imgViewone release];
    [label release];
    [imgView release];
}
#pragma mark - btn跳转更多精彩专题详情页方法
- (void)btn
{
    BtnJumpViewController *bjVC = [[BtnJumpViewController alloc] init];
    [self.navigationController pushViewController:bjVC animated:YES];
}
#pragma mark - 轻拍手势 点击进入VC方法
- (void)tapActionSec:(UITapGestureRecognizer *)tapSec
{
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    firstVC.str = _picArr[2][@"url"];
    [self.navigationController pushViewController:firstVC animated:YES];
    [firstVC release];
    
    
}
- (void)tapActionone:(UITapGestureRecognizer *)tapone
{
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    firstVC.str = _picArr[1][@"url"];
    [self.navigationController pushViewController:firstVC animated:YES];
    [firstVC release];
    

    
}
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    firstVC.str = _picArr[0][@"url"];
    [self.navigationController pushViewController:firstVC animated:YES];
    [firstVC release];
    
}
#pragma mark - 获取自定义cell数据
- (void)data
{
    NSString *str = @"http://open.qyer.com/qyer/recommands/trip?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&lat=38.88251788995828&lon=121.5401180455004&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone7%2C2&track_deviceid=0C79B4F1-2C09-4E9A-805E-05836431AB22&track_os=ios%25209.2&type=index&v=1";
    
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"%lld",downloadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 数据处理
        NSArray *arr = responseObject[@"data"];
        self.dataArr = [RootModel modelHanderWithArrar:arr];
        
        for (NSDictionary *dic in arr) {
            RootModel *m = [[RootModel alloc] init];
            
            [m setValuesForKeysWithDictionary:dic];
            
            [_dataArr addObject:m];
    
            [m release];
        }
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark - tableView方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // cell 个数
    return _dataArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    RootModel *m = _dataArr[indexPath.row];
    
    RootCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLabel.text = m.title;
    cell.titleLabel.numberOfLines = 0;
    cell.titleLabel.font = [UIFont systemFontOfSize:12.5];
    cell.nameLabel.text = m.username;
    cell.nameLabel.font = [UIFont systemFontOfSize:12.5];
    cell.lookImg.image = [UIImage imageNamed:@"yanjing"];
    cell.pinglunImg.image = [UIImage imageNamed:@"wenben"];
    cell.lookLabel.text = [m.views stringValue];
    cell.lookLabel.font = [UIFont systemFontOfSize:12.5];
    cell.pinglunLabel.text = m.replys;
    cell.pinglunLabel.font = [UIFont systemFontOfSize:12.5];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.photo]];
    

    return cell;
    
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RootModel *m = _dataArr[indexPath.row];
    
    CellViewController *cellVC = [[CellViewController alloc] init];
    
    cellVC.str = m.view_url;
    
    
    [self.navigationController pushViewController:cellVC animated:YES];
    [cellVC release];
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 90;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
